package com.google.code.dajly.model;

import java.net.MalformedURLException;
import java.util.List;
import java.util.Observable;


import junit.framework.TestCase;

public class ModelTest extends TestCase implements java.util.Observer{
	
	private Model _m;
	private static boolean refr = false;
	private static boolean refrd = false;
	
	protected void setUp() throws Exception {
		super.setUp();
		_m = new Model();
		_m.addObserver(this);
		refr = false;
		refrd = false;
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}

	public void testModel() {
		assertNotNull(_m);
	}

	public void testRefreshFeeds() {
		refr = true;
		_m.refreshFeeds();
		assertTrue(refrd);
	}
	
	public void testSetRead() {
		Entry _e = _m.getFeeds().get(0).getEntries().get(0);
		if (_e != null){
			_m.setRead(_e);
			
		}else
			fail("Cant find entry");
		
		assertTrue(_m.isRead(_e));
		
	}

	public void testIsRead() {
		Entry _e = _m.getFeeds().get(0).getEntries().get(0);
		if (_e != null){
			_m.setRead(_e);
			
		}else
			fail("Cant find entry");
		
		assertTrue(_m.isRead(_e));
	}

	public void testGetFeeds() {
		List<Feed> _feeds = _m.getFeeds();
		if(_feeds != null)
			if(_feeds instanceof List)
				if(_feeds.get(0) != null && _feeds.get(0) instanceof Feed)
					return;
		fail();
	}

	public void testGetFeedByTitle() {
		assertEquals(_m.getFeedByTitle(_m.getFeeds().get(0).getTitle()), _m.getFeeds().get(0));
	}

	public void testAddFeed() {
		try {
			_m.addFeed("http://dajly.googlecode.com/files/corriere.xml");
		} catch (MalformedURLException e) {
			fail("Malformed URL");
		}
		
		assertEquals(_m.getFeeds().get(_m.getFeeds().size()-1).getURL().toString(),"http://dajly.googlecode.com/files/corriere.xml");
	}

	public void testDeleteFeed() {
		String _current = _m.getFeeds().get(_m.getFeeds().size()-1).getURL().toString();
		_m.deleteFeed(_current);
		assertNotSame(_m.getFeeds().get(_m.getFeeds().size()-1).getURL().toString(), _current);
		
	}

	public void testRefreshFeed() {
		// TODO dopo
	}

	public void testResetTimer() {
		_m.resetTimer();
		// Non ho metodi di verifica!!!!
	}

	public void testStartTimer() {
		_m.resetTimer();
		_m.startTimer();
		
		assertNotNull(_m.getTimer());
	}

	public void testSetTimerQuantum() {
		_m.setTimerQuantum(54321);
		assertEquals(_m.getTimerQuantum(), 54321.0);
	}

	public void testSetFilter() {
		_m.setFilter(new FilterType().setFilter("Rifiuti").setType("title"));
		for(Entry _e : _m.getFeeds().get(0).getEntries()){
			assertTrue(_e.getTitle().contains("Rifiuti"));
		}
		
	}

	public void update(Observable o, Object arg) {
		if(refr)
			if(arg instanceof String)
				if(((String)arg).equals("refreshFeed"))
					refrd = true;
	}

}
